﻿using System;
using System.Net.Sockets;
using System.IO;

/// <summary>
/// Client connects to the server in order to send user input and recieve a response (the fibonacci number) if possible
/// 
/// author: Charles Williams
/// version: 2011.9.20
/// </summary>
public class Client
{
    static public void Main(string[] Args)
    {
        TcpClient socketForServer;
        try
        {
            socketForServer = new TcpClient("localHost", 12000);
        }
        catch
        {
            Console.WriteLine(
            "Failed to connect to server at {0}:12000", "localhost");
            return;
        }
        NetworkStream networkStream = socketForServer.GetStream();
        StreamReader streamReader =    new StreamReader(networkStream);
        StreamWriter streamWriter =    new StreamWriter(networkStream);
        try
        {
            string outputString;
            // read the data from the host and display it
            {
                //Send the user input
                streamWriter.WriteLine(Args[0]);
                streamWriter.Flush();

                //Recieves the fibonacci number from the server
                outputString = streamReader.ReadLine();
                Console.WriteLine("f({0})= {1}",Args[0],outputString);

            }
        }
        catch
        {
            Console.WriteLine("Exception reading from Server");
        }
        // tidy up
        networkStream.Close();
    }
}